package br.com.edumobi.fragments;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.Highlight;

import java.util.ArrayList;

import br.com.edumobi.R;


public class DesempenhoChartLinearSimuladoAlunoFragment extends GenericoFragment implements OnChartValueSelectedListener {

    private AcoesDesempenhoChartLinearFragment activity;
    private LineChart linearChart;
    private int qtdAcertos;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_desempenho_chartlinear_simulado_aluno, container, false);

        linearChart = (LineChart) view.findViewById(R.id.linearChartDesempenhoSimulado);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (activity != null) {
            activity = (AcoesDesempenhoChartLinearFragment) getActivity();

            qtdAcertos = activity.obterQtdAcertos();
        }
        carregarLinearChartDesempenhoSimulado();
    }


    @Override
    public void exibirComponentes() {
        linearChart.setVisibility(View.VISIBLE);
    }

    @Override
    public void esconderComponentes() {
        linearChart.setVisibility(View.GONE);
    }

    @Override
    public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
        Log.i("VAL SELECTED",
                "Value: " + e.getVal() + ", xIndex: " + e.getXIndex()
                        + ", DataSet index: " + dataSetIndex);
    }

    @Override
    public void onNothingSelected() {
    }

    private void carregarLinearChartDesempenhoSimulado() {

        linearChart.setOnChartValueSelectedListener(this);

        linearChart.setDrawGridBackground(false);
        linearChart.setHighlightEnabled(true);
        linearChart.setDescription(getResources().getString(R.string.titulo_chart));
        linearChart.setTouchEnabled(true);
        linearChart.setDragEnabled(true);
        linearChart.setScaleEnabled(true);
        linearChart.setPinchZoom(false);
        linearChart.resetTracking();

        Legend l = linearChart.getLegend();
        l.setPosition(LegendPosition.BELOW_CHART_LEFT);

        ArrayList<String> xVals = new ArrayList<String>();
        xVals.add("");

        ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
        ArrayList<Entry> values = new ArrayList<Entry>();

        for (int i = 0; i < qtdAcertos; i++) {

            float mult = (qtdAcertos + 1);
            float val = (qtdAcertos * mult) + 3;

            values.add(new Entry(val, i));
        }

            LineDataSet d = new LineDataSet(values, getResources().getString(R.string.nome_linear));
            d.setColor(getResources().getColor(R.color.azul_acerto));
            d.setLineWidth(3f);
            d.setHighLightColor(getResources().getColor(R.color.cinza_nao_respondidos));
            d.setCircleColor(getResources().getColor(R.color.azul_acerto));
            d.setCircleSize(4f);

            dataSets.add(d);

        dataSets.get(0).enableDashedLine(10, 10, 0);

        LineData data = new LineData(xVals, dataSets);
        linearChart.setData(data);
        linearChart.invalidate();

    }

    public interface AcoesDesempenhoChartLinearFragment extends AcoesGenericoFragment {
        int obterQtdAcertos();
    }
}
